<?php

namespace app\api\Logic\Campusclass;

use app\api\model\CampusClassDateRecordModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

//学院约课记录接口
class CampusClassDateRecordLogic
{
    /**
     * 查询班课约课记录
     * @param $params
     * @param bool $isLimit
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getRecordList($params,$isLimit = false){
        $campusClassDateRecordModel = new CampusClassDateRecordModel();
        $query = $campusClassDateRecordModel->order('createtime','desc');

        //根据班课ID筛选
        if(isset($params['date_id']) && !empty($params['date_id'])){
            $query = $query->where('date_id',$params['date_id']);
        }
        //根据用户筛选
        if(isset($params['user_id']) && !empty($params['user_id'])){
            $query = $query->where('user_id',$params['user_id']);
        }
        //根据班级ID筛选
        if(isset($params['class_id']) && !empty($params['class_id'])){
            $query = $query->where('class_id',$params['class_id']);
        }
        //预约状态筛选
        if(isset($params['status'])){
            $query = $query->where('status',$params['status']);
        }
        //根据预约时间筛选
        if(!empty($params['start_time']) && !empty($params['end_time'])){
            $query = $query->whereBetween('createtime',[$params['start_time'],$params['end_time']]);
        }

        //分页
        if($isLimit){
            $list = $query->paginate(PAGE_SIZE);
            $res = objToArray($list) ?? [];
        }else{
            $list = $query->select();
            $res['data'] = objToArray($list) ?? [];
        }

        return $res;
    }

    /**
     * 约课排队详情
     * @param $recordId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getInfo($recordId){
        $info = CampusClassDateRecordModel::where('id',$recordId)->find();
        return objToArray($info) ?? [];
    }

    /**
     * 添加约课排队记录
     * @param $params
     * @return int|string
     */
    public function insert($params){
        return CampusClassDateRecordModel::insertGetId([
            'shop_id' => $params['shop_id'] ?? 0,
            'user_id' => $params['user_id'] ?? 0,
            'class_id' => $params['class_id'] ?? 0,
            'date_id' => $params['date_id'] ?? 0,
            'status' => $params['status'] ?? 0,
            'createtime' => time()
        ]);
    }

    /**
     * 取消排队
     * @param $recordId
     * @return CampusClassDateRecordModel
     */
    public function cancel($recordId){
        $campusClassDateRecordModel = new CampusClassDateRecordModel();
        return $campusClassDateRecordModel->where('id',$recordId)
            ->update(['status' => 2]);
    }

    /**
     * 获取排在最前列的排队记录
     */
    public function getOldestInfo($dateId){
        $info = CampusClassDateRecordModel::where('date_id',$dateId)
            ->where('status',0)
            ->order('createtime','asc')
            ->find();
        return objToArray($info) ?? [];
    }
}
